<template>
  <div class="goods-comment">
    <!-- 评价的头部 -->
    <div class="head" v-if="commentInfo">
      <div class="data">
        <p><span>{{ commentInfo.salesCount }}</span><span>人购买</span></p>
        <p><span>{{ commentInfo.praisePercent }}</span><span>好评率</span></p>
      </div>
      <div class="tags">
        <div class="dt">大家都在说：</div>
        <div class="dd">
          <a v-for="(item, index) in commentInfo.tags" :key="index" href="javascript:;" @click="changeTag(index)"
            :class="{ active: currTagIndex === index }">{{ item.title }}（{{ item.tagCount }}）</a>
        </div>
      </div>
    </div>
    <div class="sort" v-if="commentInfo ">
      <span>排序：</span>
      <a href="javascript:;" @click='changeSort(null)' :class="{ active: reqParams.sortField == null }">默认</a>
      <a href="javascript:;" @click='changeSort("createTime")'
        :class="{ active: reqParams.sortField == 'createTime' }">最新</a>
      <a href="javascript:;" @click='changeSort("praiseCount")'
        :class="{ active: reqParams.sortField == 'praiseCount' }">最热</a>
    </div>
    <!-- 评价的列表 -->
    <!-- 列表 -->
    <div class="list" v-if="CommentLis">
      <div class="item" v-for="item in CommentLis" :key="item.id">
        <div class="user">
          <img :src="item.member.avatar" alt="">
          <span>{{ formatNickname(item.member.nickname) }}</span>
        </div>
        <div class="body">
          <div class="score">
            <i v-for="i in item.score" :key="i + 's'" class="iconfont icon-wjx01"></i>
            <i v-for="i in 5 - item.score" :key="i + 'k'" class="iconfont icon-wjx02"></i>
            <span class="attr">{{formatSpecs(item.orderInfo.specs)}}</span>
          </div>
          <div class="text">{{item.content}}</div>
          <!-- 评论图片组件 -->
          <GoodsCommentImag v-if="item.pictures.length" :pictures="item.pictures "></GoodsCommentImag>
          <div class="time">
            <span>{{item.createTime}}</span>
            <span class="zan"><i class="iconfont icon-dianzan"></i>{{item.praiseCount}}</span>
          </div>
        </div>
      </div>
    </div>
    <!-- 分页组件 -->
    <XtxPagination v-if="total" @current-page="changePagerFn" :total="total" :pageSize="reqParams.pageSize" :currentPage="reqParams.page"></XtxPagination>
  </div>
</template>

<script>
import { camelize, inject, reactive, ref, watch } from 'vue'
import { findGoodsComment, findGoodsCommentList } from '@/api/product'

import GoodsCommentImag from '@/views/goods/components/goods-comment-image.vue'
export default {
  name: 'GoodsComment',
  components:{
    GoodsCommentImag
  },
  setup() {
    const commentInfo = ref(null)
    const goods = inject('goods')
    findGoodsComment(goods.value.id).then(data => {
      //完善数据   往数据中追加 有图 全部评论 数据
      data.result.tags.unshift({ title: '有图', tagCount: data.result.hasPictureCount, type: 'img' })
      data.result.tags.unshift({ title: '全部评级', tagCount: data.result.evaluateCount, type: 'all' })
      commentInfo.value = data.result

    })

    // 记录当前激活的索引
    const currTagIndex = ref(0)
    const changeTag = (index) => {
      currTagIndex.value = index
      //当点击不同的tag时修改请求参数
      //情况1：全部评论
      //情况2：有图  把参数hasPicture改为true
      //情况3：其他情况 把参数hasPicture改为null 并且把tag改为相应的字段
      const tag = commentInfo.value.tags[index]
      if (tag.type == 'all') {
        reqParams.hasPicture = null
        reqParams.tag = null
      } else if (tag.type == 'img') {
        reqParams.hasPicture = true
        reqParams.tag = null
      } else {
        reqParams.hasPicture = null
        reqParams.tag = tag.title
      }
      //页码重置为1
      reqParams.page = 1
    }

    //点击排序修改
    const changeSort = (vlaue) => {
      reqParams.sortField = vlaue
      //页码重置为1
      reqParams.page = 1
    }
    //准备请求参数
    const reqParams = reactive({
      page: 1,  // 评论数据页码
      pageSize: 10,// 每页评论条数
      hasPicture: null, // 是否有图  true
      tag: null, //标签
      sortField: null //排序字段，可选值范围[praiseCount,createTime]
    })
    //使用watch侦听器  监听reqParams请求参数的变化发起网络请求
    let CommentLis = ref([])
    const total = ref(0)
    watch(reqParams, (newValue) => {

      findGoodsCommentList(goods.value.id, newValue).then(data => {
        CommentLis.value = data.result.items
        total.value = data.result.counts
      })
    }, { immediate: true })

    //评论的商品的规格属性
    const formatSpecs = (src) => {
      return src.reduce((p, c) => {
        return `${p}  ${c.name}:${c.nameValue}`
      }, '').trim()
    }

    //名称匿名处理
    const formatNickname = (src) => {
      return src.substr(0,1) + "****" + src.substr(-1)
    }
    
    //分页组件绑定的自定义事件 用于修改当前的页码值 发起网络请求
    const changePagerFn = (value) => {
      reqParams.page = value
    }

    return {
      commentInfo,
      currTagIndex,
      changeTag,
      reqParams,
      changeSort,
      CommentLis,
      formatSpecs,
      formatNickname,
      total,
      changePagerFn
    }
  }
}
</script>

<style lang="less" scoped>
.goods-comment {
  .head {
    display: flex;
    padding: 30px 0;

    .data {
      width: 340px;
      display: flex;
      padding: 20px;

      p {
        flex: 1;
        text-align: center;

        span {
          display: block;

          &:first-child {
            font-size: 32px;
            color: @priceColor;
          }

          &:last-child {
            color: #999;
          }
        }
      }
    }

    .tags {
      flex: 1;
      display: flex;
      border-left: 1px solid #f5f5f5;

      .dt {
        font-weight: bold;
        width: 100px;
        text-align: right;
        line-height: 42px;
      }

      .dd {
        flex: 1;
        display: flex;
        flex-wrap: wrap;

        >a {
          width: 132px;
          height: 42px;
          margin-left: 20px;
          margin-bottom: 20px;
          border-radius: 4px;
          border: 1px solid #e4e4e4;
          background: #f5f5f5;
          color: #999;
          text-align: center;
          line-height: 40px;

          &:hover {
            border-color: @xtxColor;
            background: lighten(@xtxColor, 50%);
            color: @xtxColor;
          }

          &.active {
            border-color: @xtxColor;
            background: @xtxColor;
            color: #fff;
          }
        }
      }
    }
  }

  .sort {
    height: 60px;
    line-height: 60px;
    border-top: 1px solid #f5f5f5;
    border-bottom: 1px solid #f5f5f5;
    margin: 0 20px;
    color: #666;

    >span {
      margin-left: 20px;
    }

    >a {
      margin-left: 30px;

      &.active,
      &:hover {
        color: @xtxColor;
      }
    }
  }
}

.list {
  padding: 0 20px;

  .item {
    display: flex;
    padding: 25px 10px;
    border-bottom: 1px solid #f5f5f5;

    .user {
      width: 160px;

      img {
        width: 40px;
        height: 40px;
        border-radius: 50%;
        overflow: hidden;
      }

      span {
        padding-left: 10px;
        color: #666;
      }
    }

    .body {
      flex: 1;

      .score {
        line-height: 40px;

        .iconfont {
          color: #ff9240;
          padding-right: 3px;
        }

        .attr {
          padding-left: 10px;
          color: #666;
        }
      }
    }

    .text {
      color: #666;
      line-height: 24px;
    }

    .time {
      color: #999;
      display: flex;
      justify-content: space-between;
      margin-top: 5px;
    }
  }
}
</style>